package com.zbkj.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.google.common.collect.Lists;
import com.zbkj.admin.service.AdminProjectStatisHandlerService;
import com.zbkj.common.enums.ProjectStatusEnum;
import com.zbkj.common.request.project.AdminProjectStatisRequest;
import com.zbkj.common.response.project.AdminProjectStatisResponse;
import com.zbkj.common.utils.LocalDateUtil;
import com.zbkj.common.vo.CountVo;
import com.zbkj.service.service.project.ProjectInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.YearMonth;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Auther:
 * @Date: 2024/8/7 18:25
 * @Description:
 */
@Service
public class AdminProjectStatisHandlerServiceImpl implements AdminProjectStatisHandlerService {
    @Autowired
    private ProjectInformationService projectInformationService;

    @Override
    public List<AdminProjectStatisResponse> prjectStatisDay(AdminProjectStatisRequest paramRequest) {
        String startDate = String.format("%s 00:00:00",paramRequest.getStartDate());
        String endDate =  String.format("%s 23:59:59",paramRequest.getEndDate());
        List<AdminProjectStatisResponse> responseList = Lists.newArrayList();
        //组装个天集合
        List<LocalDate> dateList = LocalDateUtil.getDateRange(LocalDateUtil.strToLocalDate(paramRequest.getStartDate(),LocalDateUtil.YYYY_MM_DD),LocalDateUtil.strToLocalDate(paramRequest.getEndDate(),LocalDateUtil.YYYY_MM_DD));
        for(LocalDate day : dateList) {
            AdminProjectStatisResponse response = new AdminProjectStatisResponse();
            response.setDateStr(LocalDateUtil.localDateToStr(day,LocalDateUtil.YYYY_MM_DD));
            responseList.add(response);
        }
        //按状态进行项目统计 0 待估算 20 估算中 30 估算完成 40 报告中 50 已完成
        List<CountVo> list0 = projectInformationService.prjectStatisDay(ProjectStatusEnum.TO_ESTIMATE.getStatus(),startDate,endDate);
        this.projectStatisResultBuild(ProjectStatusEnum.TO_ESTIMATE.getStatus(),responseList,list0);
        List<CountVo> list20 = projectInformationService.prjectStatisDay(ProjectStatusEnum.IN_ESTIMATE.getStatus(),startDate,endDate);
        this.projectStatisResultBuild(ProjectStatusEnum.IN_ESTIMATE.getStatus(),responseList,list20);
        List<CountVo> list30 = projectInformationService.prjectStatisDay(ProjectStatusEnum.COMPLET_ESTIMATE.getStatus(),startDate,endDate);
        this.projectStatisResultBuild(ProjectStatusEnum.COMPLET_ESTIMATE.getStatus(),responseList,list30);
        List<CountVo> list40 = projectInformationService.prjectStatisDay(ProjectStatusEnum.IN_REPORT.getStatus(),startDate,endDate);
        this.projectStatisResultBuild(ProjectStatusEnum.IN_REPORT.getStatus(),responseList,list40);
        List<CountVo> list50 = projectInformationService.prjectStatisDay(ProjectStatusEnum.FINISH.getStatus(),startDate,endDate);
        this.projectStatisResultBuild(ProjectStatusEnum.FINISH.getStatus(),responseList,list50);
        return responseList;
    }

    @Override
    public List<AdminProjectStatisResponse> prjectStatisMonth(AdminProjectStatisRequest paramRequest) {
        String startDate = String.format("%s-01 00:00:00",paramRequest.getStartDate());
        String[] endDateStrs = paramRequest.getEndDate().split("-");
        LocalDate lastEndDate = LocalDateUtil.localDateLastMothDay(Integer.valueOf(endDateStrs[0]),Integer.valueOf(endDateStrs[1]));
        String endDate =  String.format("%s 23:59:59",LocalDateUtil.localDateToStr(lastEndDate,LocalDateUtil.YYYY_MM_DD));
        String[] startDateStr = paramRequest.getStartDate().split("-");
        List<AdminProjectStatisResponse> responseList = Lists.newArrayList();
        //组装个月份集合
        List<String> dateList = LocalDateUtil.getMonthRange(YearMonth.of(Integer.valueOf(startDateStr[0]),Integer.valueOf(startDateStr[1])),
                YearMonth.of(Integer.valueOf(endDateStrs[0]),Integer.valueOf(endDateStrs[1])),LocalDateUtil.YYYY_MM);
        for(String moth : dateList) {
            AdminProjectStatisResponse response = new AdminProjectStatisResponse();
            response.setDateStr(moth);
            responseList.add(response);
        }
        //按状态进行项目统计 0 待估算 20 估算中 30 估算完成 40 报告中 50 已完成
        List<CountVo> list0 = projectInformationService.prjectStatisMonth(ProjectStatusEnum.TO_ESTIMATE.getStatus(),startDate,endDate);
        this.projectStatisResultBuild(ProjectStatusEnum.TO_ESTIMATE.getStatus(),responseList,list0);
        List<CountVo> list20 = projectInformationService.prjectStatisMonth(ProjectStatusEnum.IN_ESTIMATE.getStatus(),startDate,endDate);
        this.projectStatisResultBuild(ProjectStatusEnum.IN_ESTIMATE.getStatus(),responseList,list20);
        List<CountVo> list30 = projectInformationService.prjectStatisMonth(ProjectStatusEnum.COMPLET_ESTIMATE.getStatus(),startDate,endDate);
        this.projectStatisResultBuild(ProjectStatusEnum.COMPLET_ESTIMATE.getStatus(),responseList,list30);
        List<CountVo> list40 = projectInformationService.prjectStatisMonth(ProjectStatusEnum.IN_REPORT.getStatus(),startDate,endDate);
        this.projectStatisResultBuild(ProjectStatusEnum.IN_REPORT.getStatus(),responseList,list40);
        List<CountVo> list50 = projectInformationService.prjectStatisMonth(ProjectStatusEnum.FINISH.getStatus(),startDate,endDate);
        this.projectStatisResultBuild(ProjectStatusEnum.FINISH.getStatus(),responseList,list50);
        return responseList;
    }

    private void projectStatisResultBuild(int status, List<AdminProjectStatisResponse> responseList, List<CountVo> countList) {
        if(CollectionUtils.isEmpty(responseList) || CollectionUtils.isEmpty(countList)) {
            return;
        }
        Map<String,Integer> countVoMap = countList.stream().collect(Collectors.toMap(k->k.getDateStr(),v->v.getCountVal(),(ov,nv)->ov));
        for (AdminProjectStatisResponse vo : responseList) {
            Integer count = countVoMap.getOrDefault(vo.getDateStr(),null);
            if(Objects.nonNull(count)) {
                if(ProjectStatusEnum.TO_ESTIMATE.getStatus() == status) {
                    vo.setToEsCount(count);
                } else if(ProjectStatusEnum.IN_ESTIMATE.getStatus() == status) {
                    vo.setInEsCount(count);
                } else if(ProjectStatusEnum.COMPLET_ESTIMATE.getStatus() == status) {
                    vo.setCompletEsCount(count);
                } else if(ProjectStatusEnum.IN_REPORT.getStatus() == status) {
                    vo.setInReportCount(count);
                } else if(ProjectStatusEnum.FINISH.getStatus() == status) {
                    vo.setFinishCount(count);
                }
            }
        }
    }
}
